package com.jy.adscm.zltx.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.SjIssueRecycling;
import com.jy.adscm.zltx.service.SjIssueRecyclingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-11-08
 */
@RestController
@RequestMapping("/adscm/zltx/sjIssueRecycling")
public class SjIssueRecyclingController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    SjIssueRecyclingService sjIssueRecyclingService;
    @RequestMapping("/insert")
    public SimpleCoreResponse insert(@RequestBody SjIssueRecycling  sjIssueRecycling , HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        sjIssueRecycling.setIssuer(token.getUser().getId().toString());
        sjIssueRecycling.setAcceptTime(new Date());
        boolean b = sjIssueRecyclingService.insertSjIssueRecycling(sjIssueRecycling);
        return  new SimpleCoreResponse("200","新增成功",b);
    }
    @RequestMapping("/update")
    public SimpleCoreResponse update(@RequestBody SjIssueRecycling  sjIssueRecycling , HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (sjIssueRecycling.getRecyclingVersionCode()!=null&&!"".equals(sjIssueRecycling.getRecyclingVersionCode())){
            sjIssueRecycling.setRecyclingPerson(token.getUser().getId().toString());
            sjIssueRecycling.setRecyclingTime(new Date());
        }
        boolean b = sjIssueRecyclingService.updateById(sjIssueRecycling);
        return  new SimpleCoreResponse("200","修改成功",b);
    }
    @RequestMapping("selectByFileId/{fileId}")
    public SimpleCoreResponse selectByFileId(@PathVariable Integer fileId)throws AdscmSystemException{
       List<SjIssueRecycling> sjIssueRecyclings =sjIssueRecyclingService.selectByFileId(fileId);
        return  new SimpleCoreResponse("200","查询成功",sjIssueRecyclings);
    }
}

